package com.ph.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * @author frankwu
 * 
 */
public class DBConnection {
	private static ComboPooledDataSource dataSource;

	public static void setDataSource(ComboPooledDataSource dataSource) {
		DBConnection.dataSource = dataSource;
	}

	public static Connection getConnection() throws Exception {
		return dataSource.getConnection();
	}

	public static void insert(String aSQL) throws Exception {
		Connection conn = null;
		PreparedStatement stmt = null;

		try {
			conn = getConnection();
			conn.setAutoCommit(false);
			stmt = conn.prepareStatement(aSQL);
			stmt.execute(aSQL);

			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			try {
				if (null != conn) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void insert(List insertList) throws Exception {
		Connection conn = null;
		PreparedStatement pStmt = null;

		if (null == insertList || insertList.size() == 0) {
			throw new RuntimeException();
		}

		try {
			conn = getConnection();
			conn.setAutoCommit(false);

			for (int i = 0; i < insertList.size(); i++) {
				String aSQL = insertList.get(i).toString();
				pStmt = conn.prepareStatement(aSQL);
				pStmt.execute(aSQL);
			}

			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (pStmt != null) {
					pStmt.close();
					pStmt = null;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			try {
				if (null != conn) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static int update(String aSQL) throws Exception {
		Connection conn = null;
		PreparedStatement stmt = null;

		try {
			conn = getConnection();
			stmt = conn.prepareStatement(aSQL);
			int ret = stmt.executeUpdate(aSQL);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			try {
				if (null != conn) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void update(List updateList) throws Exception {
		Connection conn = null;
		PreparedStatement pStmt = null;

		if (null == updateList || updateList.size() == 0) {
			throw new RuntimeException();
		}

		try {
			conn = getConnection();
			conn.setAutoCommit(false);

			for (int i = 0; i < updateList.size(); i++) {
				String aSQL = updateList.get(i).toString();
				pStmt = conn.prepareStatement(aSQL);
				pStmt.executeUpdate(aSQL);
			}

			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (pStmt != null) {
					pStmt.close();
					pStmt = null;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			try {
				if (null != conn) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static int delete(String aSQL) throws Exception {
		return update(aSQL);
	}
}